import datetime

from django.db import models
from utils.modles import BaseModel

# Create your models here.

from django.contrib.auth.models import AbstractUser

class UserInfo(AbstractUser):
    phone = models.BigIntegerField(verbose_name='手机号',null=True,blank=True)
    #头像
    '''
    null=True   数据库该字段可以为空
    blank=True  admin后台管理该字段可以为空
    '''
    avatar = models.FileField(upload_to='avatar/',default='avatar/default.png',verbose_name='用户头像')
    '''
    给avatar字段传入文件对象 该文件自动会存储都avatar文件下 然后avatar字段只保存文件路径avatar/default
    '''


choices_pro = (
    ('镭射','镭射'),
    ('P8','P8'),
    ('P5','P5'),
    ('P4','P4'),
    ('FAB','FAB'),
    ('DIP','DIP'),
    ('FCT1','FCT1'),
    ('组装','组装'),
    ('注胶','注胶'),
    ('浸胶','浸胶'),
    ('涂胶','涂胶'),
    ('FCT2','FCT2'),
    ('DIP检查','DIP检查'),
)
# 1. 工程顺序记录表
class ProcessFlow(BaseModel):
    sku = models.OneToOneField(to='SKU',verbose_name='机种名',unique=True)
    proToOne = models.CharField(max_length=32,choices=choices_pro,verbose_name='工序1',null=True,blank=True)
    proToTwo = models.CharField(max_length=32,choices=choices_pro,verbose_name='工序2',null=True,blank=True)
    proToThr = models.CharField(max_length=32,choices=choices_pro,verbose_name='工序3',null=True,blank=True)
    proToFou = models.CharField(max_length=32,choices=choices_pro,verbose_name='工序4',null=True,blank=True)
    proToFiv = models.CharField(max_length=32,choices=choices_pro,verbose_name='工序5',null=True,blank=True)
    proToSix = models.CharField(max_length=32,choices=choices_pro,verbose_name='工序6',null=True,blank=True)
    proToSen = models.CharField(max_length=32,choices=choices_pro,verbose_name='工序7',null=True,blank=True)
    proToEng = models.CharField(max_length=32,choices=choices_pro,verbose_name='工序8',null=True,blank=True)
    flow = models.CharField(max_length=64,verbose_name='工艺拼接',null=True,blank=True)

    def __str__(self):
        return '%s'%self.sku.name

    class Meta:
        verbose_name = '机种工艺流程表'
        verbose_name_plural = verbose_name


# 2. 客户表
class Customer(BaseModel):
    cust_code = models.IntegerField(verbose_name='客户代码')
    full_name = models.CharField(max_length=100,verbose_name='客户全称')
    small_name = models.CharField(max_length=50,verbose_name='客户简称')
    address = models.CharField(max_length=200, verbose_name='客户地址',null=True,blank=True)
    phone = models.CharField(max_length=20,verbose_name='手机号',null=True,blank=True)
    email = models.EmailField(null=True, verbose_name='客户邮箱',blank=True)

    def __str__(self):
        return self.small_name

    class Meta:
        verbose_name = '客户表'
        verbose_name_plural = verbose_name


# 3. 机种表
class SKU(BaseModel):
    name = models.CharField(max_length=32,verbose_name='机种名',unique=True)  #机种名
    series = models.CharField(max_length=50,verbose_name='系列描述',null=True,blank=True)  #基板描述：室内机/室外机/控制板/显示板
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价',null=True,blank=True)  #机种单价，选项备注
    is_launched = models.BooleanField(default=True, verbose_name='是否上架销售')  #机种是否eol
    comments_text = models.TextField(default='', verbose_name='评价描述',null=True,blank=True)  #机种描述信息，生产注意点
    # process_flow = models.OneToOneField(to='ProcessFlow', verbose_name='工序列表')  #机种工序流程一对一关系，一个机种只有一种工艺流程
    cust = models.ForeignKey(to='Customer', verbose_name='客户简称id',blank=True,on_delete=models.CASCADE)  #客户和机种一对多关系，机种在多的一方

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '机种明细表'
        verbose_name_plural = verbose_name

# 4. 机种点数表,基板名 （包含P8+P4面）
class SkuNum(BaseModel):
    sku = models.OneToOneField(to='SKU',verbose_name='机种名id')  #机种对应点数一对一关系
    num = models.IntegerField(verbose_name='点数',null=True)  #机种总点数
    sku_pcb = models.CharField(verbose_name="使用生基板名",max_length=64,null=True)

    def __str__(self):
        return self.sku.name

    class Meta:
        verbose_name = '机种点数表'
        verbose_name_plural = verbose_name


choices_line = (
    ('LS_1L','LS1L'),
    ('LS_2L','LS2L'),
    ('LS_3L','LS3L'),
    ('SMT_1L','1L'),
    ('SMT_2L','2L'),
    ('SMT_3L','3L'),
    ('SMT_4L','4L'),
    ('SMT_5L','5L'),
    ('SMT_6L','6L'),
    ('SMT_7L','7L'),
    ('SMT_8L','8L'),
    ('SMT_9L','9L'),
    ('AI_1L','AI1L'),
    ('AI_2L','AI2L'),
    ('AI_3L','AI3L'),
    ('DIP_F','F线'),
    ('DIP_FAB','FAB线'),
    ('DIP_C','C线'),
    ('DIP_M','M线'),
    ('DIP_TJ','涂胶线'),
    ('DIP_TS','测试线'),
    ('DIP_JJ','浸胶线'),
    ('DIP_ZJ','注胶线'),
    ('DIP_ZZ','组装线'),
)

# 5. 机种_工程面_点数
class SkuProcessNumTable(BaseModel):
    '''  '''
    sku = models.ForeignKey(to='SKU',verbose_name='机种名id',on_delete=models.CASCADE)  # 一对多，一个机种多个工程面，多的一方
    process = models.CharField(max_length=32,choices=choices_pro,verbose_name='工序工程面')  # 一对多，一个机种多个工程面，多的一方 ？
    sku_process = models.CharField(max_length=64,verbose_name='机种名工程面',)
    # dianshu = models.CharField(max_length=64,verbose_name='点数',default='0')
    # sudu = models.CharField(max_length=64,verbose_name='1枚生产秒数',default='0')
    # pinban = models.CharField(max_length=64,verbose_name='1枚基板数量',default='0')

    def __str__(self):
        return '机种:%s_工程面:%s'%(self.sku.name,self.sku_process)

    class Meta:

        verbose_name = '工程面点数表'
        verbose_name_plural = verbose_name


# 6. 工程工艺表
class ProcessNum(BaseModel):
    ''' 工程工艺表 工程面 '''
    name = models.CharField(max_length=50,verbose_name='工程面') # 工程面名称
    code = models.CharField(max_length=50,verbose_name='工程面编号可以枚举') # 工程面编号ABCDE

    def __str__(self):
        return '%s_%s'%(self.name,self.code)



#机种名+工程面+线别 三者联合为唯一 这里是记录机种名+工程面对应的线别，一个机种，相同的工程面，可以在不同线别生产
# 7. 这里记录机种+工程面对应先的线别，可以在不同的线上生产
class SkuProcessLineCpa(BaseModel):
    '''机种名_工程面_线别  -->选线别'''
    sku = models.ForeignKey(to='SKU',verbose_name='机种名id',on_delete=models.CASCADE)  #机种名id
    process = models.CharField(max_length=32,choices=choices_pro,verbose_name='工程面')
    line = models.CharField(max_length=32,choices=choices_line,verbose_name='线别')
    # cap = models.IntegerField(verbose_name='一个班的产能',default='')
    sku_process = models.CharField(max_length=255,verbose_name='机种_工程面_线别拼接',default='')
    # capacity = models.IntegerField(verbose_name='班生产能力',default="")

    def __str__(self):
        return '机种%s_工程面%s_对应的%s线'%(self.sku.name,self.process,self.line)

    class Meta:
        unique_together = (
            ('sku', 'process', 'line'),  # 联合唯一,不能重复记录
        )
        verbose_name = '工程面线别表'
        verbose_name_plural = verbose_name

class Capacity(BaseModel):
    '''
    产能表
    splc是 SkuProcessLineCpa的写
    '''
    splc = models.OneToOneField(to='SkuProcessLineCpa',verbose_name='SKU工程面线别id',on_delete=models.CASCADE)
    cycle_time = models.IntegerField(verbose_name='循环时间', default=0)
    cap = models.IntegerField(verbose_name='产能', default=0)
    sheet_num = models.IntegerField(verbose_name='拼板数', default=0)

    #工程面点数表：1pc基板一个指定工程面的点数， 如果是2拼板，这里记录的是其中一个基板的一个面的点数。
    process_num = models.IntegerField(verbose_name='工程面点数表',default=0)

    def __str__(self):
        return '机种是%s，工程是%s,产线是%s产能'%(self.splc.sku.name,self.splc.sku_process,self.splc.line)

    class Meta:
        verbose_name = '产能表'
        verbose_name_plural = verbose_name



# 不需要了
# class Line(BaseModel):
#     name = models.CharField(max_length=50,verbose_name='线别')
#
#     def __str__(self):
#         return self.name


# 8. 产能表 对应的机种名+工程+线别三者联合唯一对应的产能
class Cpa(BaseModel):
    '''机种名_工程面_线别  -->对应的产能'''
    sku = models.ForeignKey(to='SKU', verbose_name='机种名id',on_delete=models.CASCADE)
    process = models.CharField(max_length=32, choices=choices_pro, verbose_name='工程面')
    line = models.CharField(max_length=32, choices=choices_line, verbose_name='线别')
    # cap = models.FloatField(verbose_name='产能',default='')
    # cycle_time = models.FloatField(verbose_name='循环时间',default='')
    sku_process_line = models.CharField(max_length=32,  verbose_name='机种_工程面_线别三者联合唯一')

    def __str__(self):
        return self.sku_process_line
    class Meta:
        unique_together = (
            ('sku', 'process', 'line'),  # 联合唯一,不能重复记录
        )
        verbose_name = '工程面线别产能表'
        verbose_name_plural = verbose_name

# 9. 订单表
class Order(BaseModel):
    code = models.CharField(max_length=64,verbose_name='工单生产指示No')
    customer = models.ForeignKey(to='Customer', verbose_name='客户', on_delete=models.CASCADE)
    sku = models.ForeignKey(to='SKU',verbose_name='机种名',on_delete=models.CASCADE)
    data_number = models.IntegerField(verbose_name='工单数量')
    item_date = models.DateField(auto_now=True,verbose_name='发行日期')
    is_delete = models.BooleanField(default=False,verbose_name='是否取消')
    order_month = models.DateField(verbose_name='工单月份', default=datetime.datetime.now().date())  #默认当天的日期
    shipping_date = models.DateField(verbose_name='出货日期',null=True,blank=True)
    is_plan = models.BooleanField(default=False,verbose_name='是否安排计划')
    is_production = models.BooleanField(default=False,verbose_name='是否开始生产')
    smt_executable_date = models.DateField(verbose_name='SMT材料到齐日', default=datetime.datetime.now().date())  #默认当天的日期
    dip_executable_date = models.DateField(verbose_name='DIP材料到齐日', default=datetime.datetime.now().date())  #默认当天的日期

    def __str__(self):
        return '机种名：%s_工单编号：%s'%(self.sku.name,self.code)

    class Meta:
        verbose_name = '订单表'
        verbose_name_plural = verbose_name


choices_work = (
    ('d','白班'),
    ('n','夜班'),
    ('dn','白夜班'),
)

choice_production_department = (
    ('制造一课','制造一课'),
    ('制造二课','制造二课'),
    ('制造三课','制造三课'),
    ('制造四课','制造四课'),
)

# 10. 生产计划表
class PlanTable(BaseModel):
    order = models.ForeignKey(to='Order',verbose_name='订单id',null=True,blank=True,on_delete=models.CASCADE)
    plan_code = models.CharField(max_length=100,verbose_name='订单指示NO',null=True,blank=True)
    customer = models.ForeignKey(to='Customer', verbose_name='客户', on_delete=models.CASCADE)
    sku = models.ForeignKey(to='SKU', verbose_name='生产机种品番', on_delete=models.CASCADE)
    Lot = models.IntegerField(verbose_name='工单数量')
    work_process = models.CharField(max_length=100, choices=choices_pro, verbose_name='生产工程面')
    plan_pro_code = models.CharField(max_length=64, verbose_name='工程生产指示NO', null=True, blank=True)
    line = models.CharField(max_length=100, choices=choices_line, verbose_name='生产线别')
    date_num = models.DateField(verbose_name='生产日期',default=datetime.datetime.now().date())  #默认当天的日期
    work_num = models.CharField(max_length=100,choices=choices_work,verbose_name='生产班次',default='d')  #白班计划 or夜班计划
    work_int = models.IntegerField(verbose_name='生产数量')
    memo = models.CharField(verbose_name='说明', blank=True, null=True, max_length=100)
    working_hours = models.FloatField(verbose_name='完成计划时间',null=True,blank=True)
    change_time = models.FloatField(default=0.3,verbose_name='机种切换时间')
    production_line_num = models.IntegerField(verbose_name='线别生产顺序',default=0) #计划安排顺序排序用
    check_production_cpa = models.FloatField(verbose_name='班检查能力',null=True,blank=True)  #一个班的实装检查产能
    check_time = models.FloatField(verbose_name='检查当前Lot时间',null=True,blank=True)
    production_department = models.CharField(max_length=100,verbose_name='制造部门',choices=choice_production_department,default='制造一课')
    edit = models.CharField(max_length=255,verbose_name='编辑',null=True,blank=True) #计划修改使用
    spd = models.DateField(verbose_name='交货日期',null=True,blank=True)

    class Meta:
        verbose_name = '计划表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s机种%s面计划'%(self.sku.name,self.work_process)

#工程对应的生产部门，不唯一不好区分
class ProcessProductionDepartment(BaseModel):
    process = models.CharField(max_length=100,choices=choices_pro,verbose_name='工程名称')
    department = models.CharField(max_length=100,choices=choice_production_department,verbose_name='制造部门')

    class Meta:
        verbose_name = '工程对应生产部门'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '工程:%s,制造部门:%s'%(self.process,self.department)

#产线对应的生产部门
class LineProductionDepartment(BaseModel):
    line = models.CharField(max_length=100,verbose_name='生产线',choices=choices_line)
    department = models.CharField(max_length=100, choices=choice_production_department, verbose_name='制造部门')

    class Meta:
        verbose_name = '产线对应生产部门'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '产线:%s,制造部门:%s'%(self.line,self.department)


record_type_choices=(
    ('1','生产计划'),
    ('2','返品记录'),
    ('3','客户改造'),
)

# 生产日报明细表
class DailyProduction(BaseModel):
    sku = models.ForeignKey(to='SKU',verbose_name='机种名',on_delete=models.DO_NOTHING)
    plan_table = models.ForeignKey(to='PlanTable',verbose_name='计划id',null=True,blank=True,on_delete=models.DO_NOTHING)
    order = models.ForeignKey(to='Order',verbose_name='订单id',on_delete=models.DO_NOTHING,null=True,blank=True)
    line = models.CharField(max_length=100, choices=choices_line, verbose_name='生产线别')
    work_process = models.CharField(max_length=100, choices=choices_pro, verbose_name='生产工程')
    completed_quantity = models.IntegerField(verbose_name='生产数量',default=0)
    number_of_personnel = models.FloatField(verbose_name='生产人数',default=1)
    number_of_hours = models.FloatField(verbose_name='生产时间',default=0)
    defective_qty = models.IntegerField(verbose_name='不良数量',default=0)
    manufacturing_scrap = models.IntegerField(verbose_name='制造报废',default=0)
    technical_scrap = models.IntegerField(verbose_name='技术报废',default=0)
    analysis_scrap = models.IntegerField(verbose_name='解析报废',default=0)
    item_scrap = models.IntegerField(verbose_name='单品报废',default=0)
    excellent_quantity = models.IntegerField(verbose_name='良品数量',max_length=0)

    record_type = models.CharField(verbose_name='记录类型',max_length=100,default=1,choices=record_type_choices) # 计划记录，返品记录，客户改造
    production_department = models.CharField(max_length=100, verbose_name='制造部门',
                                             choices=choice_production_department, default='制造一课')
    date_num = models.DateField(verbose_name='生产日期', default=datetime.datetime.now().date())  # 默认当天的日期
    smwh = models.FloatField(verbose_name='实绩单台', null=True,blank=True)  # 实际人数和时间计算实绩单台
    standard_wh = models.FloatField(verbose_name='标准单台', null=True,blank=True)  # 当前机种，当前工程标准单台工时
    is_qualified = models.BooleanField(verbose_name='是否合格',default=True)  # 当前机种当前工程的实绩单挑时间是否达标
    work_num = models.CharField(max_length=32,verbose_name='班次', choices=choices_work,default='d')
    memo = models.TextField(verbose_name='备注', blank=True, null=True)
    class Meta:
        verbose_name = '生产日报'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '生产日报表'


# DIP标准单台工时
class ST(BaseModel):
    customer = models.ForeignKey(to='Customer', verbose_name='客户', on_delete=models.DO_NOTHING,null=True,blank=True)
    sku = models.ForeignKey(to='SKU',verbose_name='机种名')
    pro = models.CharField(verbose_name='DIP工程',choices=choices_pro,max_length=64)
    people_number = models.FloatField(verbose_name='标准人数')
    hour_production = models.IntegerField(verbose_name='1小时产量')
    st = models.FloatField(verbose_name='工程标准单台/分钟')  # 一小时完成300台，单台工时 300/60 =

    def __str__(self):
        return '标准工时表'

    class Meta:
        unique_together = (
            ('sku', 'pro',),  # 联合唯一,不能重复记录
        )
        verbose_name = '标准工时表'
        verbose_name_plural = verbose_name


# 工单完成情况日报累计数量表
class DailyStatus(BaseModel):
    sku = models.ForeignKey(to='SKU', verbose_name='机种名')
    order = models.ForeignKey(to='Order',verbose_name='工单id')
    process = models.CharField(max_length=100, choices=choices_pro, verbose_name='工程名称',null=True,blank=True)
    Lot = models.IntegerField(verbose_name='工单数量')
    accumulated_completed_qty = models.IntegerField(verbose_name='累计完成总量',default=0)
    accumulated_defects_qty = models.IntegerField(verbose_name='累计不良数',default=0)
    accumulated_scrap_qty = models.IntegerField(verbose_name='累计报废数量',default=0)
    #记录最后的完成情况 工单数 = 完成数 + 报废数  不良数不参与
    department = models.CharField(max_length=100, choices=choice_production_department, verbose_name='制造部门')

    class Meta:
        unique_together = (
            ('sku', 'order','process',),  # 联合唯一,不能重复记录
        )
        verbose_name = '日报完成累计表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '日报完成累计表'




























